<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2021/11/17
 * Time: 16:57
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

declare(strict_types=1);

namespace app\common\business;

use app\common\lib\Show;
use app\common\lib\Status;
use app\common\model\mysql\Yu as ModelObj;
use app\common\validate\Yu as ValObj;
use think\Exception;

class Yu extends Base
{
    protected $valObj = null;
    protected $modelObj = null;

    public function __construct()
    {
        $this->valObj = new ValObj();
        $this->modelObj = new ModelObj();
    }

    public function getHomeByIndexPage(string $fatherOpenid = "", array $where = [])
    {
        try {
            $list = $this->modelObj
                ->where($where)
                ->where('fatherOpenid', $fatherOpenid)
                ->where('status', Status::mysql_normal)
                ->order('id desc')
                ->paginate(10, true)->each(function ($item, $index) {
                    $wechat = (new Wechat())->getOpenidById($item['openid']);
                    $item['head'] = $wechat['avatarUrl'] ?? "";
                    $item['nickname'] = $wechat['nickName'] ?? "";
                    $item['wechat_id'] = $wechat['id'] ?? "";

                    return $item;
                });
        } catch (\Exception $e) {
            throw new Exception("查询失败");
        }
        return $list;
    }

    public function getHomeByCount(string $openid = "", array $where = [])
    {
        try {
            $list = $this->modelObj
                ->field('count(*) as count')
                ->where($where)
                ->where('fatherOpenid', $openid)
                ->where('status', Status::mysql_normal)
                ->find();
        } catch (\Exception $e) {
            throw new Exception("查询失败");
        }
        return $list['count'];
    }

}